package com.ridbor001.demo.servlet;

import com.ridbor001.demo.bean.RDBGrade;
import com.ridbor001.demo.bean.RDBUser;
import com.ridbor001.demo.service.RDBCourseService;
import com.ridbor001.demo.service.RDBGradeService;
import com.ridbor001.demo.service.RDBStudentService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;

@WebServlet("/addGrade")
public class RDBAddGradeServlet extends HttpServlet {
    RDBGradeService gradeService = new RDBGradeService();
    RDBCourseService courseService = new RDBCourseService();
    RDBStudentService studentService = new RDBStudentService();
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {

        // 权限验证
        RDBUser user = (RDBUser) req.getSession().getAttribute("user");
        if (user == null) {
            resp.sendRedirect("/login");
        }
        if (user != null && !user.isTeacher()) {
            resp.sendError(403, "无操作权限");
            return;
        }

        // 参数获取与校验
        String studentId = req.getParameter("studentId");
        String courseId = req.getParameter("courseId");

        try {
            if(!courseService.courseExists(courseId)) {
                req.setAttribute("error", "课程不存在");
                req.getRequestDispatcher("/error.jsp").forward(req, resp);
                return;
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        try {
            if(!studentService.studentExists(studentId)) {
                req.setAttribute("error", "学生不存在");
                req.getRequestDispatcher("/error.jsp").forward(req, resp);
                return;
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }


        double dailyScore = Double.parseDouble(req.getParameter("dailyScore"));
        double stageScore = Double.parseDouble(req.getParameter("stageScore"));
        double finalScore = Double.parseDouble(req.getParameter("finalScore"));
        String term = req.getParameter("term");


        if (dailyScore < 0 || dailyScore > 100 ||
                stageScore < 0 || stageScore > 100 ||
                finalScore < 0 || finalScore > 100) {
            req.setAttribute("error", "成绩需在0-100之间");
            req.getRequestDispatcher("/error.jsp").forward(req, resp);
            return;
        }
        // 创建成绩对象
        RDBGrade grade = new RDBGrade(
                studentId,
                courseId,
                term,
                dailyScore,
                stageScore,
                finalScore
        );
        try {
            gradeService.addGrade(grade);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

        resp.sendRedirect("gradeManage");
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String studentId = req.getParameter("studentId");
        String courseId = req.getParameter("courseId");

        req.setAttribute("studentId", studentId);
        req.setAttribute("courseId", courseId);
        req.getRequestDispatcher("/rDBAddGradeForm.jsp").forward(req, resp);

    }
}